<?php
error_reporting(E_ALL | E_STRICT);
header('Content-Type: text/plain');
ini_set("memory_limit", "120M");
// chdir("../sample_images/") or die('Cannot change directory!');
// echo getcwd();
// echo "\n";
// $path="/home/brightfi/public_html/lucastest/testing/";
// $path="testing";
// if (!file_exists($path)) {
// echo mkdir($path);
// }
// die();
// chdir("/home/brightfi/public_html/globe/images/sample_images/set1");
// echo getcwd();
// print_r(glob("*"));
// chdir("/home/brightfi/public_html/globe/images/sample_images/set1");
class DirectoryScanner
{
    private $thumbWidth;
    private $thumbHeight;
    // List of allowable extensions
    private $extensions = array('jpg', 'jpeg', 'png', 'gif');
    // Directory names  to skip
    private $dirsToSkip = array('.', '..', 'thumbs');
    public function __construct($path, $thumbWidth = 200, $thumbHeight = NULL)
    {
        chdir($path) or die('Cannot change directory!');
        $this->thumbWidth  = $thumbWidth;
        $this->thumbHeight = $thumbHeight;
    }
    private function scan($path)
    {
        $pathToThumbs = $path . '/thumbs';
        $dir          = opendir($path);
        while (false !== ($fname = readdir($dir))) {
            // $info         = pathinfo( $fname );
            $ext          = pathinfo($fname, PATHINFO_EXTENSION);
            $fullFilePath = $path . '/' . $fname;
            // print_r( $info );
            if (is_dir($fullFilePath)) {
                if (!in_array($fname, $this->dirsToSkip)) {
                    $this->scan($fullFilePath);
                }
            } else if (is_file($fullFilePath) && in_array($ext, $this->extensions)) { // Create thumbnail
                echo "Creating thumbnail for {$fullFilePath} \n";
                // load image and get image size
                if ($ext == 'jpg' || $ext == 'jpeg') {
                    $img = imagecreatefromjpeg($fullFilePath);
                } elseif ($ext == 'png') {
                    $img = imagecreatefrompng($fullFilePath);
                } elseif ($ext == 'gif') {
                    $img = imagecreatefromgif($fullFilePath);
                }
                $width      = imagesx($img);
                $height     = imagesy($img);
                // echo $width."   ".$height;
                // calculate thumbnail size
                $new_height = is_null($this->thumbHeight) ? ceil($height * ($this->thumbWidth / $width)) : $this->thumbHeight;
                // create a new temporary image
                $tmp_img    = imagecreatetruecolor($this->thumbWidth, $new_height);
                // copy and resize old image into new image
                imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $this->thumbWidth, $new_height, $width, $height);
                if (!file_exists($pathToThumbs)) {
                    mkdir($pathToThumbs);
                    echo "Creating directory {$pathToThumbs} \n";
                }
                // save thumbnail into a file
                imagejpeg($tmp_img, $pathToThumbs . '/' . $fname);
                // Free up memory
                imagedestroy($tmp_img);
            }
        }
        closedir($dir);
    }
    public function run_scan()
    {
        $this->scan('.');
    }
}

// $path = '/home/brightfi/public_html/flexiant_blog/wp-content/themes/pagelines/';
$path = '../sample_images/';
$ds   = new DirectoryScanner($path);
$ds->run_scan();

?>